package com.atguigu.zerocopy;


import java.io.*;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;

public class NewIOServer {
    public static void main(String[] args) throws Exception {
        InetSocketAddress address = new InetSocketAddress(7002);
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();

        ServerSocket serverSocket = serverSocketChannel.socket();
        serverSocket.bind(address);

        ByteBuffer byteBuffer = ByteBuffer.allocate(4096);

        while (true) {
            SocketChannel socketChannel = serverSocketChannel.accept();
            int readCount = 0;
             //output1(socketChannel, byteBuffer);
            while (-1 != readCount) {
                try {
                    readCount = socketChannel.read(byteBuffer);
                    System.out.println(byteBuffer);
                } catch (Exception e) {
                    e.printStackTrace();
                    break;
                }
                byteBuffer.rewind();
            }
        }
    }

    static void output2(SocketChannel socketChannel,ByteBuffer buf) throws IOException {

        File file = new File("D:\\data\\aa\\22.mp4");
        FileOutputStream out = new FileOutputStream(file);

        // 打开FileChannel
        RandomAccessFile aFile = new RandomAccessFile("D:\\data\\aa\\22.mp4","rw");
        FileChannel channel = aFile.getChannel();
        int readCount = 0;
        int len = 0;
        int count = 0;
        int anInt = -1;
        boolean statusRead = false;
        while (-1 != readCount) {
            try {
                readCount = socketChannel.read(buf);
                System.out.println(buf);

                if (!statusRead && buf.limit() > 0) {
                    //读取第一位标识成功或失败的 Int 值
                    anInt = buf.getInt();
                    statusRead = true;
                }
                if (anInt == 1) {
                    if (len > 0) {
                        count += buf.limit() - buf.position();
                    }
                    //输出到本地磁盘
                    out.write(buf.array(), buf.position(), buf.limit() - buf.position());

                } else if (anInt == 0) {
                    //失败
                    System.out.println("服务端请求图片资源失败!");
                    System.out.println(new String(buf.array(), buf.position(), buf.limit() - buf.position(), StandardCharsets.UTF_8));
                }
                buf.flip();
                buf.clear();
            } catch (Exception e) {
                e.printStackTrace();
                break;
            }
            buf.rewind();
        }
    }
    static void output1(SocketChannel socketChannel,ByteBuffer buf) throws IOException {

        File file = new File("D:\\data\\aa\\22.mp4");
        FileOutputStream out = new FileOutputStream(file);
        int readCount = 0;
        int len = 0;
        int count = 0;
        int anInt = -1;
        boolean statusRead = false;
        while (-1 != readCount) {
            try {
                readCount = socketChannel.read(buf);
                System.out.println(buf);
                buf.flip();
                if (!statusRead && buf.limit() > 0) {
                    //读取第一位标识成功或失败的 Int 值
                    anInt = buf.getInt();
                    statusRead = true;
                }
                if (anInt == 1) {
                    if (len > 0) {
                        count += buf.limit() - buf.position();
                    }
                    //输出到本地磁盘
                    out.write(buf.array(), buf.position(), buf.limit() - buf.position());

                } else if (anInt == 0) {
                    //失败
                    System.out.println("服务端请求图片资源失败!");
                    System.out.println(new String(buf.array(), buf.position(), buf.limit() - buf.position(), StandardCharsets.UTF_8));
                }
                buf.clear();
            } catch (Exception e) {
                e.printStackTrace();
                break;
            }
            buf.rewind();
        }
    }
    static void output(SocketChannel sChannel,ByteBuffer buf) throws IOException {
        //1. 获取通道



        //2. 创建指定大小的缓冲区


        // 主动关闭输出但不关闭通道，通知服务端当前发送请求已经完毕，等待服务端响应

        File file = new File("D:\\data\\aa\\22.mp4");
        FileOutputStream out = new FileOutputStream(file);
        int len = 0;
        int count = 0;
        int anInt = -1;
        boolean statusRead = false;
        //循环读取服务端返回到通道的数据，直到达到末尾
        while ((len = sChannel.read(buf)) != -1) {
            buf.flip();
            if (!statusRead && buf.limit() > 0) {
                //读取第一位标识成功或失败的 Int 值
                anInt = buf.getInt();
                statusRead = true;
            }
            if (anInt == 1) {
                if (len > 0) {
                    count += buf.limit() - buf.position();
                }
                //输出到本地磁盘
                out.write(buf.array(), buf.position(), buf.limit() - buf.position());

            } else if (anInt == 0) {
                //失败
                System.out.println("服务端请求图片资源失败!");
                System.out.println(new String(buf.array(), buf.position(), buf.limit() - buf.position(), StandardCharsets.UTF_8));
            }
            buf.clear();
        }
        out.close();
        System.out.println("返回最终大小：" + count);
        //关闭通道
        sChannel.close();
    }
}
